package com.icow.wms.basic.controller;

import com.icow.wms.basic.domain.*;
import com.icow.wms.basic.scheduled.EnterAllFunc;
import com.icow.wms.basic.service.IDoorPeripheralService;
import com.icow.wms.basic.service.ILiftPeripheralService;
import com.icow.wms.basic.service.ISsxPeripheralService;
import com.icow.wms.basic.utils.DoorHandler;
import com.icow.wms.basic.utils.LiftHandler;
import com.icow.wms.basic.utils.PLCUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Slf4j
@Controller
@SuppressWarnings("all")
@RequestMapping("/api/peripheralJob")
public class PeripheralJobController {

    @Autowired
    private ISsxPeripheralService ssxPeripheralService;
    @Autowired
    private IDoorPeripheralService doorPeripheralService;
    @Autowired
    private ILiftPeripheralService liftPeripheralService;

    @ResponseBody
    @PostMapping("/ssx")
    public PeripheralJobResponse peripheralJobSSX(@RequestBody PeripheralDomain peripheralDomain) {
        long start = System.currentTimeMillis();
        //log.info("输送线对接:" + peripheralDomain);
        SsxPeripheral ssxPeripheral = new SsxPeripheral();
        ssxPeripheral.setDockLocation(peripheralDomain.getLocationName());
        List<SsxPeripheral> ssxPeripherals = ssxPeripheralService.selectSsxPeripheralList(ssxPeripheral);
        if (ssxPeripherals.size() != 1) {
            return new PeripheralJobResponse("false", peripheralDomain.getLocationName() + " size not correct,please check database");
        }
        //拿到输送线对接的所有数据
        ssxPeripheral = ssxPeripherals.get(0);
        AgvTask agvTask = new AgvTask();
        agvTask.setInnerTaskName(peripheralDomain.getOrderName());
        agvTask.setAgvName(peripheralDomain.getVehicleName());
        if (!EnterAllFunc.loadFinishTasks.contains(agvTask)) {
            //取货对接
            boolean loadRequest = PLCUtils.writePLC(ssxPeripheral.getIp(), ssxPeripheral.getLoadRequest(), true);
            boolean loadAllow = PLCUtils.readPLC(ssxPeripheral.getIp(), ssxPeripheral.getLoadAllow());
            if (loadRequest && loadAllow) {
                if (PLCUtils.writePLC(ssxPeripheral.getIp(), ssxPeripheral.getLoadRequest(), false)) {
                    return new PeripheralJobResponse("true", "loadRequest:" + loadRequest + "loadAllow:" + loadAllow);
                }
            }
        } else {
            //放货对接
            boolean unloadRequest = PLCUtils.writePLC(ssxPeripheral.getIp(), ssxPeripheral.getUnloadRequest(), true);
            boolean unloadAllow = PLCUtils.readPLC(ssxPeripheral.getIp(), ssxPeripheral.getUnloadAllow());
            if (unloadRequest && unloadAllow) {
                if (PLCUtils.writePLC(ssxPeripheral.getIp(), ssxPeripheral.getUnloadRequest(), false)) {
                    return new PeripheralJobResponse("true", "unloadRequest:" + unloadRequest + "unloadAllow:" + unloadAllow);
                }
            }
        }
        long end = System.currentTimeMillis();
        log.info(ssxPeripheral.getIp() + " 输送线对接耗时：" + (end - start) + "ms");
        return new PeripheralJobResponse("false", "ssx not allow");
    }

    @ResponseBody
    @PostMapping("/door")
    public PeripheralJobResponse peripheralJobDoor(@RequestBody PeripheralDomain peripheralDomain) {
        long start = System.currentTimeMillis();
        //log.info("门对接:" + peripheralDomain);
        DoorPeripheral doorPeripheral = new DoorPeripheral();
        doorPeripheral.setDockLocation(peripheralDomain.getLocationName());
        List<DoorPeripheral> doorPeripherals = doorPeripheralService.selectDoorPeripheralList(doorPeripheral);
        if (doorPeripherals.size() != 1) {
            return new PeripheralJobResponse("false", peripheralDomain.getLocationName() + " size not correct,please check database");
        }
        //拿到门对接的所有数据
        doorPeripheral = doorPeripherals.get(0);
        //发送开门信号以及读取对应的门开信号
        Door door = DoorHandler.IP_DOOR_MAP.get(doorPeripheral.getIp());
        if (door == null) {
            return new PeripheralJobResponse("false", doorPeripheral.getIp() + " door not online");
        }
        door.clearDoorState();
        boolean openDoor = door.openDoor(doorPeripheral.getIp(), doorPeripheral.getOpenDoor());
        boolean doorState = door.getDoorState(doorPeripheral.getIp(), doorPeripheral.getDoorUp());
        if (openDoor && doorState) {
            Door closeDoor = new Door();
            closeDoor.setIp(doorPeripheral.getIp());
            closeDoor.setCloseDoorPosition(doorPeripheral.getCloseDoorPosition());
            closeDoor.setPipe(doorPeripheral.getOpenDoor());
            closeDoor.setVehicleName(peripheralDomain.getVehicleName());
            door.canClearDoorState();//对接结束，关闭该门状态哈哈
            log.info(closeDoor + " door thread boot");
            new Thread(closeDoor).start();
            return new PeripheralJobResponse("true", doorPeripheral.getIp() + " door open ");
        }
        long end = System.currentTimeMillis();
        log.info(doorPeripheral.getIp() + " door docking time:" + (end - start) + "ms");
        return new PeripheralJobResponse("false", doorPeripheral.getIp() + " door not allow");
    }

    @ResponseBody
    @PostMapping("/lift")
    public PeripheralJobResponse peripheralJobLift(@RequestBody PeripheralDomain peripheralDomain) {
        long start = System.currentTimeMillis();
        //log.info("电梯对接:" + peripheralDomain);
        LiftPeripheral liftPeripheral = new LiftPeripheral();
        liftPeripheral.setDockLocation(peripheralDomain.getLocationName());
        List<LiftPeripheral> liftPeripherals = liftPeripheralService.selectLiftPeripheralList(liftPeripheral);
        if (liftPeripherals.size() != 1) {
            return new PeripheralJobResponse("false", peripheralDomain.getLocationName() + " size not correct,please check database");
        }
        //拿到电梯对接的所有数据
        liftPeripheral = liftPeripherals.get(0);
        Lift lift = LiftHandler.IP_LIFT_MAP.get(liftPeripheral.getIp());
        if (lift == null) {
            return new PeripheralJobResponse("false", liftPeripheral.getIp() + " lift not online");
        }
        //电梯状态初始化
        lift.init(liftPeripheral.getCallLiftCommand(), liftPeripheral.getLiftReachCommand(), liftPeripheral.getStopCallLiftCommand(), liftPeripheral.getFloor());
        //呼叫电梯 并获取电梯的状态
        boolean callLift = lift.callLift(liftPeripheral.getIp(), liftPeripheral.getCallLiftCommand());
        boolean liftReach = lift.readLiftState(liftPeripheral.getIp(), liftPeripheral.getReadLiftReachCommand(), Integer.parseInt(liftPeripheral.getFloor()));
        if (callLift && liftReach) {
            Lift stopCallLift = new Lift();
            stopCallLift.setIp(liftPeripheral.getIp());
            stopCallLift.setCloseLiftPosition(liftPeripheral.getStopCallLiftPosition());
            stopCallLift.setStopCallLiftCommand(liftPeripheral.getStopCallLiftCommand());
            stopCallLift.setCallLiftCommand(liftPeripheral.getCallLiftCommand());
            stopCallLift.setVehicleName(peripheralDomain.getVehicleName());
            lift.clearDoorState();
            log.info(stopCallLift + " lift thread boot");
            new Thread(stopCallLift).start();
            return new PeripheralJobResponse("true", liftPeripheral.getIp() + " callLift:" + callLift + " liftReach:" + liftReach);
        }
        long end = System.currentTimeMillis();
        log.info(liftPeripheral.getIp() + " lift docking time:" + (end - start) + "ms");
        return new PeripheralJobResponse("false", "lift not allow");
    }

}
